/*
1：安装axios   npm i axios
2:导入axios   import axios from 'axios'
3:创建axios副 本
  const _axios=axios.create({
baseURL:process.env.VUE_APP_BASE_API
  })
4:创建请求拦截与响应拦截
5:暴露出去

*/

import router from '@/router'
import store from '@/store'
import axios from 'axios'
import { Message } from 'element-ui'
const _axios = axios.create({
  baseURL: process.env.VUE_APP_BASE_API
})
// 请求拦截器
_axios.interceptors.request.use(
  config => {
    config.headers.Authorization = `Bearer ${store.state.user.token}`
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
_axios.interceptors.response.use(
  res => {
    if (res.data.success) {
      return res.data
    } else {
      Message.error(res.data.message)
      return Promise.reject(res.data.message)
    }
  },
  error => {
    console.dir(error)
    if (error.response.status === 401) {
      store.commit('user/loginout')
      router.push('/login?result=' + self.location.href.split("#")[1])
      alert('登录超时')
    }
    return Promise.reject(error)
  }
)
export default _axios
